let { foodMaterials } =require('../shop/gameData')

cc.Class({
    extends: cc.Component,

    properties: {
      list:cc.Node,
      materialPrefab:cc.Prefab,
      stockPanel:cc.Node
    },


    onLoad () {
        this.makeList();
    },
    makeList () {
       if(this.list.children ){
        this.list.children.forEach(val=>{
            val.destroy();
        })
       }
        for (var i = 0; i <foodMaterials.length; i++) {
                    let item  = cc.instantiate(this.materialPrefab);
                    this.list.addChild(item);
                    //  {
    //     id:'bingkuai',name:'冰块',saveTime:999,cost:0.1,transport:1,stock:0,ice:0.1
    // },
                    item.getChildByName("name").getComponent(cc.Label).string = `${foodMaterials[i].name}`
                    item.getChildByName("price").getComponent(cc.Label).string = `${foodMaterials[i].cost*100}元/100份`
                    item.getChildByName("time").getComponent(cc.Label).string = `配送：${foodMaterials[i].transport}小时`
                    item.getChildByName("stateTime").getComponent(cc.Label).string = `保质期：${foodMaterials[i].saveTime}小时`
                    let stock = {space:1,type:'binggui'};
                    if(foodMaterials[i].stock==0){
                        stock.type ='binggui';stock.space = foodMaterials[i].ice;
                    }else{
                        stock.type ='cangku';stock.space = foodMaterials[i].stock;

                    }
                    cc.loader.loadRes(`pic/${stock.type}.png`,cc.SpriteFrame,function(err,spriteFrame){
                        item.getChildByName("space").getChildByName("spaceIcon").getComponent(cc.Sprite).spriteFrame  = spriteFrame;
                        item.getChildByName("space").getComponent(cc.Label).string = `空间：${stock.space*100}`
                    });
                    this.list.height +=100;
                    item.id = foodMaterials[i].id;
        }
        cc.log(this.list)
    },
    start () {

    },
    showShopStock (target,id) {
        this.stockPanel.active = true;
        cc.log(target.target.id);
        window.materialId = target.target.id;
    }
    // update (dt) {},
});
